//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package ooo.reindeer.data.decoder;

import io.netty.buffer.ByteBuf;

import java.nio.ByteOrder;


/**
 * 使用可变长度整型长度字段帧解码器
 *
 * @author songbailin
 * @date 2022/08/26
 */
public class UintVarLengthFieldFrameDecoder extends LengthFieldBasedFrameDecoder {

    public UintVarLengthFieldFrameDecoder(int maxFrameLength, int lengthFieldOffset) {
        this(maxFrameLength, lengthFieldOffset, 0, 0, true);
    }

    public UintVarLengthFieldFrameDecoder(int maxFrameLength, int lengthFieldOffset, int lengthAdjustment) {
        this(maxFrameLength, lengthFieldOffset, lengthAdjustment, 0, true);
    }

    public UintVarLengthFieldFrameDecoder(int maxFrameLength, int lengthFieldOffset, int lengthAdjustment, int initialBytesToStrip) {
        this(maxFrameLength, lengthFieldOffset, lengthAdjustment, initialBytesToStrip, true);
    }

    public UintVarLengthFieldFrameDecoder(int maxFrameLength, int lengthFieldOffset, int lengthAdjustment, int initialBytesToStrip, boolean failFast) {
        super(maxFrameLength, lengthFieldOffset, 1, lengthAdjustment, initialBytesToStrip, failFast);

    }

    @Override
    protected long getUnadjustedFrameLength(ByteBuf buf, int offset, int length, ByteOrder order) {

        int index = offset;
        int b = buf.getUnsignedByte(index);
        if ((b & 0x80) == 0) {
            return b;
        }
        long result = b & 0x7F;
        int shift = 7;
        while ((b & 0x80) != 0) {
            b = buf.getUnsignedByte(++index);
            result |= (long) (b & 0x7F) << shift;
            shift += 7;
        }

        return result;

    }


}
